perf(textkit): improve line slicing performance#3420
Open
wojtekmaj wants to merge 1 commit into
Open
Conversation
🦋 Changeset detectedLatest commit: a2b98ae The changes in this PR will be included in the next version bump. This PR includes changesets to release 9 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
There was a problem hiding this comment.
Pull request overview
This PR improves @react-pdf/textkit line slicing performance for long paragraphs while preserving existing line-breaking behavior.
Changes:
- Optimize ligature offset calculation by scanning only the local repeated-index cluster in
offset. - Replace the linear backward scan in
glyphIndexAtwith a binary search overglyphIndices. - Avoid repeated array copying in
breakLinesby accumulating lines viapush.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| packages/textkit/src/run/offset.ts | Reduces ligature offset computation from prefix slicing/filtering to a bounded backward scan. |
| packages/textkit/src/run/glyphIndexAt.ts | Uses binary search to find the last glyph mapping for a given string index. |
| packages/textkit/src/engines/linebreaker/index.ts | Builds the lines array via mutation (push) instead of repeated spreading during accumulation. |
| .changeset/smart-maps-pay.md | Adds a patch changeset describing the performance improvement. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
2165382 to
a2b98ae
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This improves textkit line slicing performance for long paragraphs without changing line-breaking behavior.
Changes:
glyphIndexAtinstead of scanning backward through the full glyph index arraybreakLineswithpushinstead of copying the accumulated line array on every breakpointMeasured in a downstream PDF generator with a single long left-aligned body paragraph:
Together with #3421, this improves performance in the same downstream PDF generator by:
The memory profile is mostly unchanged with this PR alone because the dominant heap allocation comes from the Knuth-Plass active-node search, but the CPU/wall-time improvement is still measurable.